<?php
class Page{
	private $cfg = array(
			'currentPage' => 1,
			'itemPerPage' => 15,
			'totalItems' => 0,
			'range' => 5,
			'prevBtn' => '&laquo;',
			'nextBtn' => '&raquo;',
			);	
	public $pages;

	public function __construct( $cfg="" ){
		if( is_array($cfg) ) $this->config($cfg);
	}

	private function config( $cfg ){
		if( is_array($cfg) ){
			$this->cfg = array_merge($this->cfg,$cfg);
		}
	}

	public function init( $cfg="" ){
		if( is_array($cfg) ) $this->config($cfg);
		if( $this->cfg['itemPerPage']>=$this->cfg['totalItems'] ){
			return false;
		}
		$return = array();
		$this->pages = ceil($this->cfg['totalItems']/$this->cfg['itemPerPage']);
		$start = ($this->cfg['currentPage']-$this->cfg['range'])>1 ? $this->cfg['currentPage']-$this->cfg['range'] : 1;
		$end = ($this->pages-$this->cfg['currentPage']) > $this->cfg['range'] ? $this->cfg['currentPage']+$this->cfg['range'] : $this->pages;
		$prev = array('label'=>$this->cfg['prevBtn']);
		if( $this->cfg['currentPage']<=1 ){
			$prev['disabled'] = 1;
		}else{
			$prev['page'] = $this->cfg['currentPage'] - 1;
		}
		$return[0] = $prev;
		for( $i=1,$j=$start; $j<=$end; $i++,$j++ ){
			$return[$i]['page'] = $j;
			$return[$i]['label'] = $j;
			if( $j==$this->cfg['currentPage'] ) $return[$i]['index'] = 1;
		}
		$next = array('label'=>$this->cfg['nextBtn']);
		if( $this->cfg['currentPage']>=$this->pages ){
			$next['disabled'] = 1;
		}else{
			$next['page'] = $this->cfg['currentPage'] + 1;
		}
		$return[$this->pages+1] = $next;
		return $return;
	}

}

